package cn.itlaobing.web.servlet;

import cn.itlaobing.service.Impl.StainServiceImpl;
import cn.itlaobing.service.StatinService;
import cn.itlaobing.web.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

/**
 * Created by admin on 2017/11/10.
 */
@WebServlet("/stationServlet/*")
public class StatinServlet extends BaseServlet {
    @RequestMapping("/query")
    public void query(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
        //设置字符编码
        request.setCharacterEncoding("utf-8");
        //得到参数 String startStation, String endStation, String trainNum
        String startStation=request.getParameter("startStation");
        String endStation=request.getParameter("endStation");
        String trainNum=request.getParameter("trainNum");
        if (startStation==null || "".equals(startStation)){
            request.setAttribute("MSG","起始地点不能为空");
            request.getRequestDispatcher("/query.jsp").forward(request,response);
            return;
        }
        if (endStation==null || "".equals(endStation)){
            request.setAttribute("MSG","终点起始地点不能为空");
            request.getRequestDispatcher("/query.jsp").forward(request,response);
            return;
        }
        if (trainNum==null || "".equals(trainNum)){
            request.setAttribute("MSG","车次不能为空");
            request.getRequestDispatcher("/query.jsp").forward(request,response);
            return;
        }
        //调用业务方法
        StatinService statinService=new StainServiceImpl();
        try {
            Map<String,Object> map=statinService.queryAll(startStation,endStation,trainNum);
            request.getSession().setAttribute("MAP",map);
            request.getRequestDispatcher("/WEB-INF/view/show.jsp").forward(request,response);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
